﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SubSonic;
using CXDB;
using Coolite.Ext;
using Coolite.Ext.Web;
public partial class Provider_Modify : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) { Store1Databind(); Store3Bind(); }
    }
    protected void Store1Databind()
    {
        Query q = new Query(CxProvider.Schema);
        q.SetSelectList("id,providername,devicename,dept_master_status,tech_master_status,office_master_status,boss_status,intime");
        q.AddWhere("entering_userid",Cookies.getCookies("cuid"));
        q.AddWhere("dept_master_status",2);
        q.ORDER_BY("id desc");
        Store1.DataSource=q.ExecuteDataSet();
        Store1.DataBind();
    }
    protected void Store1_OnRefreshData(object s, StoreRefreshDataEventArgs e)
    {
        Store1Databind();
    }
    protected void DeleteRow(object s, AjaxEventArgs e)
    {
        string id = e.ExtraParams["id"].ToString();
        CxProvider.Delete("id", id);
        Store1Databind();
        Err.Warning("删除成功！",MessageBox.Icon.NONE);
    }
    protected void RowSelect(object s, AjaxEventArgs e)
    {
        string id = e.ExtraParams["id"].ToString();
        Query q = new Query(CxProvider.Schema);
        q.AddWhere("id", id);
        IDataReader dr = q.ExecuteReader();
        if (dr.Read())
        {
            tfxinghui_detail.Text = dr["xinghu_detail"].ToString();
            tfxinghu_time.Value = dr["xinghu_time"].ToString();
            tfxinghu_project.Text = dr["xinghu_project"].ToString();
            tfshame_detail.Text = dr["sham_detail"].ToString();
            tfsham_time.Value = dr["sham_time"].ToString();
            tfsham_project.Text = dr["sham_project"].ToString();
            TfProviderName.Text = dr["providername"].ToString();
            tfother_detail.Text = dr["other_detail"].ToString();
            tfid.Value = id;
            tfguid.Value = dr["guid"].ToString();
            TfDeviceName.Text = dr["devicename"].ToString();
            tfdept_master_id.SetInitValue(dr["dept_master_id"].ToString());
            tfcontract_time.Value = dr["contract_time"].ToString();
            tfcontract_project.Text = dr["contract_project"].ToString();
            tfcontract_detail.Text = dr["contract_detail"].ToString();
        }
    }
    protected void SubmitForm(object s, AjaxEventArgs e)
    {
        if (tfid.Value.ToString() == "") { Err.Warning("请从左侧选择一条记录！", MessageBox.Icon.WARNING); return; }
         
        CxProvider p = new CxProvider("id",tfid.Value.ToString());
        p.ProviderName = TfProviderName.Text;
        p.DeviceName = TfDeviceName.Text;
        if (tfsham_time.Value.ToString() != "0001/1/1 0:00:00")
        {
            p.ShamTime = DateTime.Parse(tfsham_time.Value.ToString());
        }
        p.ShamProject = tfsham_project.Text;
        p.ShamDetail = tfshame_detail.Text;
        if (tfxinghu_time.Value.ToString() != "0001/1/1 0:00:00")
        {
            p.XinghuTime = DateTime.Parse(tfxinghu_time.Value.ToString());
        }
        p.XinghuProject = tfxinghu_project.Text;
        p.XinghuDetail = tfxinghui_detail.Text;
        if (tfcontract_time.Value.ToString() != "0001/1/1 0:00:00")
        {
            p.ContractTime = DateTime.Parse(tfcontract_time.Value.ToString());
        }
        p.ContractProject = tfcontract_project.Text;
        p.ContractDetail = tfcontract_detail.Text;
        p.OtherDetail = tfother_detail.Text;
        //p.EnteringUserid = int.Parse(Cookies.getCookies("cuid"));
        //p.DeptMasterId = int.Parse(tfdept_master_id.SelectedItem.Value.ToString());
        p.DeptMasterStatus = 0;
         
        p.InTime = DateTime.Now; 
        
        p.Save();
        CXFileUpload.Del(tfguid.Value.ToString());
        CXFileUpload.Sv(tffileupload, new Guid(tfguid.Value.ToString()));
        
        //清空form
        tfxinghui_detail.Text = "";
        tfxinghu_time.Value = "";
        tfxinghu_project.Text = "";
        tfshame_detail.Text = "";
        tfsham_time.Value = "";
        tfsham_project.Text = "";
        TfProviderName.Text = "";
        tfother_detail.Text = "";
        tfid.Value = "";
        tfguid.Value = "";
        TfDeviceName.Text = "";
        tfdept_master_id.Reset();
        tfcontract_time.Value = "";
        tfcontract_project.Text = "";
        tfcontract_detail.Text = "";
        Ext.MessageBox.Alert("成功", "已经提交至相关领导审核").Show();
    }
    protected void Store3Bind()
    {
        int deptId = int.Parse(Cookies.getCookies("cDeptID"));
        if (deptId > 10)
        {
            Query q = new Query(CxSysDept.Schema);
            q.SetSelectList("deptid");
            q.AddWhere("ID", deptId);
            deptId = int.Parse(q.ExecuteScalar().ToString());
        }
        Query q2 = new Query(CxUserBase.Schema);
        q2.SetSelectList("username,uID");
        q2.AddWhere("levelid", 1);
        q2.AddWhere("depid", deptId);
        Store3.DataSource = q2.ExecuteDataSet();
        Store3.DataBind();
    }
    protected int getDeptId(int deptId)
    {
        if (deptId > 10)
        {
            Query q = new Query(CxSysDept.Schema);
            q.SetSelectList("deptid");
            q.AddWhere("ID", deptId);
            deptId = int.Parse(q.ExecuteScalar().ToString());
        }
        return deptId;
    }
}
